package com.test.springboot.springBootUtils.util;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.util.StringUtils;

import com.test.springboot.springBootUtils.bean.CaseMode;
import com.test.springboot.springBootUtils.bean.CheckCase;

/**
 * 自定义的校验 单词是否大小写的 具体实现
 * @author My
 *
 */
public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> {
	private CaseMode caseMode;

	@Override
	public void initialize(CheckCase constraintAnnotation) {
		this.caseMode = constraintAnnotation.value();
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		if (StringUtils.isEmpty(value)) {
			return true;
		}
		if (caseMode == CaseMode.UPPER) {
			return value.equals(value.toUpperCase());
		} else {
			return value.equals(value.toLowerCase());
		}
	}

}
